package com.woniuxy.web.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.*;
import com.woniuxy.service.ModelItemService;
import com.woniuxy.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/model")
@CrossOrigin(value="http://192.168.6.186:8080",allowCredentials = "true")
public class ModelController_b {
    @Autowired
    private ModelService modelService;
    @Autowired
    private ModelItemService modelItemService;

    @PostMapping("add")
    public ResponseEntity add(@RequestBody Model model){
//        System.out.println(name);
            modelService.add(model);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("pageList")
    public ResponseEntity<List<Model>> pageList(Integer current, Integer pageSize,Model model){
        if(current == null || current == 0){
            current=1;
        }
        if(pageSize == null || pageSize == 0){
            pageSize=2;
        }

        QueryWrapper<Model> queryWrapper = new QueryWrapper<>();
        if (model.getName() != null && model.getName().trim().length() != 0) {
            queryWrapper.like("name", model.getName());
        }
        Page<Model> page = new Page<>(current, pageSize);
        page = modelService.page(page, queryWrapper);
        return new ResponseEntity(page);
    }

    @PostMapping("delete")
    public ResponseEntity delete(@RequestBody Model model){
        int modelId = model.getId();
        modelItemService.deleteByModelId(modelId);
        modelService.removeById(modelId);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("list")
    public ResponseEntity<List<Model>> list(){

        return new ResponseEntity<List<Model>>(modelService.getAll());
    }
}
